package com.lego.jbv;

import lejos.nxt.I2CPort;
import lejos.nxt.UltrasonicSensor;

public class Scanner extends Thread {
	UltrasonicSensor sensor;
	final private int readingSize = 20;
	private int shortestDistance;
	private int[] readings;
	public Scanner(I2CPort port)
	{
		sensor = new UltrasonicSensor(port);

		readings = new int[readingSize];
	}
	
	public void run()
	{
		int counter = 0;
		while(true)
		{
			readings[counter%readingSize] = sensor.getDistance();
			if(counter>10000)
				counter = 0;
		}
	}
	
	public synchronized int getShortestDistance()
	{

		shortestDistance = 255;
		for(int i = 0; i<readingSize; i++)
		{
			if(readings[i]<shortestDistance && readings[i] != 0)
				shortestDistance = readings[i];
		}
		return shortestDistance;
	}
	
}
